package com.zipon.tpf.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.annotation.SysLog;
import com.mysiteforme.admin.util.LayerData;
import com.mysiteforme.admin.util.RestResponse;
import com.zipon.tpf.entity.TpfTcdirs;
import com.zipon.tpf.service.TpfTcdirsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.Map;

/**
 * <p>
 * 用例目录表  前端控制器
 * </p>
 *
 * @author zipon
 * @since 2018-08-26
 */
@Controller
@RequestMapping("/tpf/tpfTcdirs")
public class TpfTcdirsController {
    private static final Logger LOGGER = LoggerFactory.getLogger(TpfTcdirsController.class);

    @Autowired
    private TpfTcdirsService tpfTcdirsService;

    @GetMapping("list")
    @SysLog("跳转用例目录表列表")
    public String list(){
        return "/tpf/tpfTcdirs/list";
    }

    @PostMapping("list")
    @ResponseBody
    @SysLog("请求用例目录表列表数据")
    public LayerData<TpfTcdirs> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<TpfTcdirs> layerData = new LayerData<>();
        EntityWrapper<TpfTcdirs> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag",false);
        if(!map.isEmpty()){
        }
        Page<TpfTcdirs> pageData = tpfTcdirsService.selectPage(new Page<>(page,limit),wrapper);
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增用例目录表页面")
    public String add(){
        return "/tpf/tpfTcdirs/add";
    }

    @PostMapping("add")
    @SysLog("保存新增用例目录表数据")
    @ResponseBody
    public RestResponse add(TpfTcdirs tpfTcdirs){
        tpfTcdirsService.insert(tpfTcdirs);
        return RestResponse.success();
    }

    @GetMapping("edit")
    @SysLog("跳转编辑用例目录表页面")
    public String edit(Long id,Model model){
        TpfTcdirs tpfTcdirs = tpfTcdirsService.selectById(id);
        model.addAttribute("tpfTcdirs",tpfTcdirs);
        return "/tpf/tpfTcdirs/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑用例目录表数据")
    public RestResponse edit(TpfTcdirs tpfTcdirs){
        if(null == tpfTcdirs.getId() || 0 == tpfTcdirs.getId()){
            return RestResponse.failure("ID不能为空");
        }
        tpfTcdirsService.updateById(tpfTcdirs);
        return RestResponse.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除用例目录表数据")
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        TpfTcdirs tpfTcdirs = tpfTcdirsService.selectById(id);
        tpfTcdirs.setDelFlag(true);
        tpfTcdirsService.updateById(tpfTcdirs);
        return RestResponse.success();
    }

}